#include "mycoin.h"


MyCoin::MyCoin(QString imgpath)
{
    QPixmap pix;
    bool ret=pix.load(imgpath);
    if(!ret){
        QString str=QString ("图片加在失败，失败的路径为：%1").arg(imgpath);
        qDebug()<<str;
        return;
    }
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

    //创建定时器，不然就是野指针
    this->timer1=new QTimer(this);
    this->timer2=new QTimer(this);

    //监听定时器1
    connect(this->timer1,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/new/prefix1/res/Coin000%1.png").arg(this->min++);
        bool ret=pix.load(str);
        if(!ret){
            QString str1=QString ("图片加在失败，失败的路径为：%1").arg(str);
            qDebug()<<str1;
            return;
        }
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));

        //已经到最后一张图
        if(this->min>this->max){
            this->min=1;
            this->timer1->stop();
            isAnimation=false;
        }
    });


    //监听定时器2
    connect(this->timer2,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/new/prefix1/res/Coin000%1.png").arg(this->max--);
        bool ret=pix.load(str);
        if(!ret){
            QString str1=QString ("图片加在失败，失败的路径为：%1").arg(str);
            qDebug()<<str1;
            return;
        }
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));

        //已经到最后一张图
        if(this->max<this->min){
            this->max=8;
            this->timer2->stop();
            isAnimation=false;
        }
    });
}

void MyCoin::changeFlag()
{
    if(this->flag){
        this->timer1->start(30);//开启计时器
        this->flag=false;
    }
    else
    {
        this->timer2->start(30);
        this->flag=true;
    }
    isAnimation=true;
}

//动画期间，禁止点击
//游戏胜利也要禁止
void MyCoin::mousePressEvent(QMouseEvent *e)
{
    if(this->isAnimation||this->isWin){
        return;
    }
    else{
        QPushButton::mousePressEvent(e);
    }
}
